package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.mapper.UserInfoMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    SystemUserService systemUserService;

    @Autowired
    SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getCaptcha() {
        Captcha captcha = new SpecCaptcha(150,50,5);
        String text = captcha.text().toLowerCase();
        String imageCode = captcha.toBase64();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString().replace("-","");
        // redis
        redisTemplate.opsForValue().set(key,text,60, TimeUnit.SECONDS);

        // 前台
        CaptchaVo captchaVo = new CaptchaVo(imageCode,key);
        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {

        // 验证验证码，删除
        String captchaCode = loginVo.getCaptchaCode();
        String captchaKey = loginVo.getCaptchaKey();
        Assert.hasText(captchaCode,"验证码不能为空");
        Assert.hasText(captchaKey,"验证码key不能为空");
        String captchaCodeCache = redisTemplate.opsForValue().get(captchaKey);
        Assert.isTrue(StringUtils.hasText(captchaCodeCache)&&captchaCodeCache.equals(captchaCode),"验证码错误或者过期");
        redisTemplate.delete(captchaKey);

        // 验证用户名和密码
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        Assert.isTrue(StringUtils.hasText(username)&&StringUtils.hasText(password),"用户名或密码不可以为空");
        QueryWrapper<SystemUser> systemUserQueryWrapper = new QueryWrapper<>();
        systemUserQueryWrapper.eq("username",username);
        SystemUser systemUser = systemUserMapper.selectOne(systemUserQueryWrapper);
        Assert.notNull(systemUser,"用户名不存在");
        password = DigestUtils.md5Hex(password);
        Assert.isTrue(systemUser.getPassword().equals(password),"密码错误");

        //  生成token
        String token =JwtUtil.createToken(systemUser.getId(), systemUser.getUsername());

        return token;
    }

    @Override
    public SystemUser getSystemUser(Long userId) {
        SystemUser systemUser = systemUserMapper.selectById(userId);
        return systemUser;
    }
}
